package com.easyCode.dataMock;

import cn.hutool.extra.spring.SpringUtil;
import lombok.Data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author 大忽悠
 * @create 2023/1/18 11:26
 */
@Data
public class TableMockInfo {
    private String tableName;
    private int mockNum;
    private List<FieldMockInfo> fieldMockInfoList;

    public Collection<String> getInsertList() {
        //没有任何字段参与mock
        if(fieldMockInfoList.isEmpty()){
            return Collections.emptyList();
        }
        List<String> insertSqlList=new ArrayList<>(mockNum);
        for (int i = 0; i < mockNum; i++) {
             insertSqlList.add(getOneInsertSql());
        }
        return insertSqlList;
    }

    /**
     * @return insert into tableName(fieldName1,fieldName2,...) values(val1,val2...)
     */
    private String getOneInsertSql() {
        List<ColumnHolder> columnHolderList=new ArrayList<>();
        for (FieldMockInfo fieldMockInfo : fieldMockInfoList) {
            ColumnHolder columnHolder = new ColumnHolder();
            columnHolder.setColumnName(fieldMockInfo.getFieldName());
            columnHolder.setColumnVal(DataMockManager.getDataGenerator(fieldMockInfo.getRule()).generator(fieldMockInfo));
            columnHolderList.add(columnHolder);
        }
        return SpringUtil.getBean(MockSqlParse.class).mockSql(tableName,columnHolderList);
    }

    @Data
    public static class ColumnHolder{
        private String columnName;
        private Object columnVal;
    }
}
